function LookupAddress()
{
var aoItems = crmForm.all.customerid.DataValue;
if (aoItems == null)
{
var sArg = crmForm.GetLabel(crmForm.all.customerid);
alert( formatString(LOCID_PROVIDE_VALUE_ADDRESS, sArg));
return;
}

var o = openStdDlg(prependOrgName("/sfa/quotes/dlg_lookupaddress.aspx?headerForm=1&parentType=" + aoItems[0].type + "&parentId=" + aoItems[0].id + "&willCall=" + (crmForm.all.willcall.DataValue ? "1" : "0")), "LookupAddress", 500, 330, true);

if (o)
{
SetBillTo(o);
SetShipTo(o, false, parseInt(crmFormSubmit.crmFormSubmitObjectType.value, 10));
}
}


function LookupDetailAddress()
{
if (!crmForm.all.willcall.DataValue)
{
var iHeaderType;
var sHeaderId;

switch (parseInt(crmFormSubmit.crmFormSubmitObjectType.value, 10))
{
case QuoteDetail:

iHeaderType	= Quote;
sHeaderId	= crmForm.quoteid.value;

break;

case SalesOrderDetail:

iHeaderType	= SalesOrder;
sHeaderId	= crmForm.salesorderid.value;

break;

case InvoiceDetail:

iHeaderType	= Invoice;
sHeaderId	= crmForm.invoiceid.value;

break;
}

var o = openStdDlg(prependOrgName("/sfa/quotes/dlg_lookupaddress.aspx?headerForm=0&headerType=" + iHeaderType + "&headerId=" + sHeaderId), "LookupAddress", 500, 330, true);

if (o)
{
SetShipTo(o, true);
}
}
}


function SetBillTo(o)
{
with (crmForm)
{
if (o.BillTo)
{
billto_name.value				= o.Address.Name;
billto_line1.value				= o.Address.Line1;
billto_city.value				= o.Address.City;
billto_stateorprovince.value	= o.Address.StateOrProvince;
billto_postalcode.value			= o.Address.PostalCode;
billto_country.value			= o.Address.Country;
billto_telephone.value			= o.Address.Telephone;
billto_fax.value				= o.Address.Fax;

if(!IsNull(crmForm.billto_addressid))
{
billto_addressid.value			= o.Address.AddressId;
}

if(!IsNull(crmForm.billto_contactname))
{
billto_contactname.value		= o.Address.ContactName;
}

if(!IsNull(crmForm.billto_line2))
{
billto_line2.value				= o.Address.Line2;
}

if(!IsNull(crmForm.billto_line3))
{
billto_line3.value				= o.Address.Line3;
}
}
}
}


function SetShipTo(o, isDetail, iObjectType)
{
with (crmForm.all)
{
if (o.ShipTo)
{
shipto_name.value				= o.Address.Name;
shipto_line1.value				= o.Address.Line1;
shipto_city.value				= o.Address.City;
shipto_stateorprovince.value	= o.Address.StateOrProvince;
shipto_postalcode.value			= o.Address.PostalCode;
shipto_country.value			= o.Address.Country;
shipto_telephone.value			= o.Address.Telephone;
shipto_fax.value				= o.Address.Fax;

if(!IsNull(crmForm.shipto_addressid))
{
shipto_addressid.value			= o.Address.AddressId;
}

if(!IsNull(crmForm.shipto_contactname))
{
shipto_contactname.value		= o.Address.ContactName;
}

if(!IsNull(crmForm.shipto_line2))
{
shipto_line2.value				= o.Address.Line2;
}

if(!IsNull(crmForm.shipto_line3))
{
shipto_line3.value				= o.Address.Line3;
}

if (isDetail)
{
shipto_freighttermscode.DataValue = o.Address.FreightTerms;
}
else
{
if (iObjectType != Invoice)
{
freighttermscode.DataValue = o.Address.FreightTerms;
}

shippingmethodcode.DataValue = o.Address.ShippingMethod;
}
}
}
}
